<?php

namespace app\api\controller;

use app\admin\model\Department;
use app\common\BaseHelper;

use PhpOffice\PhpSpreadsheet\IOFactory;
use think\Db;

use app\admin\model\Image;
use think\Exception;
use think\Request;
class Guide extends BaseController
{
    /**
     * 指南列表
     *
     * @return \think\Response
     */
    public function getGuideList()
    {
        $param = self::getHttpParam();
        $model = db('guidetoaffairs');
        if (empty($param->type)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'type 不能为空');
        }
        $where = [
            'status' => 1,
            'categoryId'   => $param->type
        ];
        $fields = "id,imgUrl,title";
        $result = $model->where($where)->field($fields)->select();
        $total = count($result);
        if ($result){
            return showTotal(config('status.SUCCESS_STATUS'),self::MSG_SUCCESS,$result,$total);
        }
        return show(config('status.ERROR_STATUS'),self::NOT_DATA,'暂无数据');
    }

    /*
     * 指南详情
     * */
    public function getGuideDetail()
    {
        $param = self::getHttpParam();
        if (empty($param->guideId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'guideId不能为空');
        }
        $where = [
            'id' => $param->guideId
        ];
        $model = db('guidetoaffairs');
        $fields = 'a.id , a.imgUrl,a.detail,a.createDate,a.title,a.readCount,u.p_name';
        $result = \app\admin\model\Guidetoaffairs::alias('a')
            //->join('user u','a.createOper = u.id')
            ->where($where)
            ->find();
        $createName = \app\admin\model\User::where(['id'=>$result['createOper']])->field('p_name')->find();
        $result['createName'] = $createName['p_name'];
        if(!$result['source']){
            $result['source'] = $this->defaultPublisher;
        }
        //阅读量 +1
        $result['readCount'] = $result['readCount'] + 1;
        $model->where($where)->update(['readCount'=> $result['readCount']],true);

        if ($result){
            return show(config('status.SUCCESS_STATUS'),self::MSG_SUCCESS,$result);
        }

        return show(config('status.ERROR_STATUS'),self::NOT_DATA,'暂无数据');
    }

}
